package com.example.yuejian.yuejian.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.yuejian.yuejian.R;
import com.example.yuejian.yuejian.base.BaseActivity;
import com.example.yuejian.yuejian.base.ModleListener;
import com.example.yuejian.yuejian.bean.UsersInfo;
import com.example.yuejian.yuejian.fragment.ClubFragment;
import com.example.yuejian.yuejian.fragment.HomeFragment;
import com.example.yuejian.yuejian.fragment.ShowPhotoFragment;
import com.example.yuejian.yuejian.fragment.TrainFragment;
import com.example.yuejian.yuejian.model.UserModel;
import com.example.yuejian.yuejian.view.RoundImageView;
import com.example.yuejian.yuejian.view.SlidingMenu;
import com.squareup.picasso.Picasso;

import butterknife.ButterKnife;


public class MainActivity extends BaseActivity implements View.OnClickListener {

    private SlidingMenu mLeftMenu;

    private LinearLayout mTabHome;
    private LinearLayout mTabShowPhoto;
    private LinearLayout mTabTrain;
    private LinearLayout mTabClub;

    private ImageButton mImgHome;
    private ImageButton mImgShowPhoto;
    private ImageButton mImgTrain;
    private ImageButton mImgClub;

    private Fragment mFmHome;
    private Fragment mFmShowPhoto;
    private Fragment mFmTrain;
    private Fragment mFmClub;

    private TextView mTvHome;
    private TextView  mTvShowPhoto;
    private TextView mTvTrain;
    private TextView mTvClub;

    private Toolbar mTbHeadBar;
    private DrawerLayout mMyDrawable;
    ActionBarDrawerToggle mToggle;
    private  LinearLayout mLlMenu;

    private RelativeLayout rl_myprize;
    private RoundImageView myself_userphoto;
    private TextView myself_username;
    private RelativeLayout rl_myfollow;
    private RelativeLayout rl_order;
    private RelativeLayout rl_myshow;
    private  LinearLayout ll_setting;
    private  TextView about_yuejian;
    private ImageView btn_search;
    private ModleListener<UsersInfo> modleListener=new ModleListener<UsersInfo>() {
        @Override
        public void onResponse(UsersInfo o) {
            UsersInfo res=o;
            if(!"".equals(res.getPhoto()))
            {
                Picasso.with(getApplication()).load(res.getPhoto()).into(myself_userphoto);
            }
            myself_username.setText(res.getNickname());
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(getApplication(),"cuowu",Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        initData();
        myself();
        setSelect(0);

    }
    public  void toggleMenu(View view)
    {
        mLeftMenu.toggle();
    }
    private  void getuserInfo()
    {
        UserModel userModel=new UserModel();
      userModel.userInfo(getUser_id(),modleListener);
    }
    private void myself()
    {


        rl_myprize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MyPrizeActivity.class);
                startActivity(intent);
            }
        });

        rl_myfollow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent=new Intent(MainActivity.this,MyFollowActivity.class);
            startActivity(intent);
        }
    });
        rl_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MyOrderActivity.class);
                startActivity(intent);
            }
        });
        rl_myshow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MyShowPhotoActivity.class);
                startActivity(intent);
            }
        });
        ll_setting.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,MySettingActivity.class);
                startActivity(intent);
            }
        });
        about_yuejian.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,AboutActivity.class);
                startActivity(intent);
            }
        });
        btn_search.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent=new Intent(MainActivity.this,SearchActivity.class);
                startActivity(intent);
            }
        });

    }

    private void initEvent() {
        mTabHome.setOnClickListener(this);
        mTabShowPhoto.setOnClickListener(this);
        mTabTrain.setOnClickListener(this);
        mTabClub.setOnClickListener(this);
    }

    private void initView() {
        //mLeftMenu=(SlidingMenu)findViewById(R.id.id_menu);

        ButterKnife.bind(this);

        mTabHome = (LinearLayout) findViewById(R.id.id_tab_home);
        mTabShowPhoto = (LinearLayout) findViewById(R.id.id_tab_showphone);
        mTabClub = (LinearLayout) findViewById(R.id.id_tab_club);
        mTabTrain = (LinearLayout) findViewById(R.id.id_tab_train);

        mImgHome = (ImageButton) findViewById(R.id.id_tab_home_img);
        mImgShowPhoto = (ImageButton) findViewById(R.id.id_tab_showphone_img);
        mImgClub = (ImageButton) findViewById(R.id.id_tab_club_img);
        mImgTrain= (ImageButton) findViewById(R.id.id_tab_train_img);

        mTvHome=(TextView)findViewById(R.id.id_tab_home_tv);
        mTvShowPhoto=(TextView)findViewById(R.id.id_tab_showphone_tv);
        mTvClub=(TextView)findViewById(R.id.id_tab_club_tv);
        mTvTrain=(TextView)findViewById(R.id.id_tab_train_tv);

        mTbHeadBar= (Toolbar) findViewById(R.id.tbHeadBar);
        mLlMenu= (LinearLayout) findViewById(R.id.llMenu);
        mMyDrawable= (DrawerLayout) findViewById(R.id.dlMenu);

        initToolBarAndDrawableLayout();
        mMyDrawable.closeDrawers();/*收起抽屉*/
        rl_myprize=(RelativeLayout)findViewById(R.id.id_my_prize_leftmenu);
        myself_userphoto=(RoundImageView) findViewById(R.id.sildmenu_userphoto);
        myself_username=(TextView)findViewById(R.id.sildmenu_username);
        rl_myfollow=(RelativeLayout)findViewById(R.id.id_my_follow_leftmenu);
        rl_order=(RelativeLayout)findViewById(R.id.id_my_order_leftmenu);
        ll_setting=(LinearLayout)findViewById(R.id.ll_sildmenu_setting_btn);
        rl_myshow=(RelativeLayout)findViewById(R.id.id_my_showphoto_leftmenu);
        about_yuejian=(TextView)findViewById(R.id.sildmenu_about_yuejian);
        btn_search=(ImageView)findViewById(R.id.id_search);

        //mViewPager = (ViewPager) findViewById(R.id.id_viewpager);
    }
    private void initToolBarAndDrawableLayout() {
        setSupportActionBar(mTbHeadBar);
        /**
        设置toolbar的list
         */
      mTbHeadBar.setNavigationIcon(R.drawable.ic_menu_black_24dp);
  /*以下俩方法设置返回键可用*/
     /*  getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/
  /*设置标题文字不可显示*/
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        mToggle = new ActionBarDrawerToggle(this, mMyDrawable, mTbHeadBar, R.string.app_name, R.string.app_name) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                //Toast.makeText(MainActivity.this, R.string.open, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                //Toast.makeText(MainActivity.this, R.string.close, Toast.LENGTH_SHORT).show();
            }
        };
  /*mMyDrawable.setDrawerListener(mToggle);不推荐*/
//        mMyDrawable.addDrawerListener(mToggle);
        //       mToggle.syncState();/*同步状态*/
    }


    @Override
    public void onClick(View v) {

        resetImgs();
        resetTvColor();
        switch (v.getId())
        {
            case R.id.id_tab_home:
                setSelect(0);
                break;
            case R.id.id_tab_showphone:
                setSelect(1);
                break;
            case R.id.id_tab_club:
                setSelect(2);
                break;
            case R.id.id_tab_train:
                setSelect(3);
                break;

            default:
                break;
        }

    }

    private void setSelect(int i) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        hideFragment(transaction);

        switch (i)
        {
            case 0:
                if (mFmHome == null)
                {
                    mFmHome = new HomeFragment();
                    transaction.add(R.id.id_content, mFmHome);
                } else
                {
                    transaction.show(mFmHome);
                }
                mImgHome.setImageResource(R.drawable.ic_home_pressed);
                mTvHome.setTextColor(mTvHome.getResources().getColor(R.color.green));


                break;
            case 1:
                if (mFmShowPhoto == null)
                {
                    mFmShowPhoto = new ShowPhotoFragment();transaction.add(R.id.id_content, mFmShowPhoto);
                } else
                {
                    transaction.show(mFmShowPhoto);

                }
                mImgShowPhoto.setImageResource(R.drawable.ic_showphone_pressed);
                mTvShowPhoto.setTextColor(mTvShowPhoto.getResources().getColor(R.color.green));
                break;
            case 2:
                if (mFmClub == null)
                {
                    mFmClub = new ClubFragment();
                    transaction.add(R.id.id_content, mFmClub);
                } else
                {
                    transaction.show(mFmClub);
                }
                mImgClub.setImageResource(R.drawable.ic_club_pressed);
                mTvClub.setTextColor(mTvClub.getResources().getColor(R.color.green));
                break;
            case 3:
                if (mFmTrain == null)
                {
                    mFmTrain = new TrainFragment();
                    transaction.add(R.id.id_content, mFmTrain);
                } else
                {
                    transaction.show(mFmTrain);
                }
                mImgTrain.setImageResource(R.drawable.ic_train_pressed);
                mTvTrain.setTextColor(mTvTrain.getResources().getColor(R.color.green));
                break;

            default:
                break;
        }
       // mViewPager.setCurrentItem(i);
        transaction.commit();
    }

    private void hideFragment(FragmentTransaction transaction) {
        if (mFmHome != null)
        {
            transaction.hide(mFmHome);
        }
        if (mFmShowPhoto != null)
        {
            transaction.hide(mFmShowPhoto);
        }
        if (mFmClub != null)
        {
            transaction.hide(mFmClub);
        }
        if (mFmTrain != null)
        {
            transaction.hide(mFmTrain);
        }
    }

    private void resetImgs() {
        mImgHome.setImageResource(R.drawable.ic_home);
        mImgShowPhoto.setImageResource(R.drawable.ic_showphone);
        mImgTrain.setImageResource(R.drawable.ic_train);
        mImgClub.setImageResource(R.drawable.ic_club);
    }
    private void resetTvColor() {
        mTvHome.setTextColor(mTvHome.getResources().getColor(R.color.white));
        mTvShowPhoto.setTextColor(mTvShowPhoto.getResources().getColor(R.color.white));
        mTvClub.setTextColor(mTvClub.getResources().getColor(R.color.white));
        mTvTrain.setTextColor(mTvTrain.getResources().getColor(R.color.white));

    }
    private void initData(){
       /* mFragments = new ArrayList<Fragment>();
        //Fragment加入集合中
        mFragments.add(new HomeFragment());
        mFragments.add(new ShowPhotoFragment());
        mFragments.add(new SearchFragment());
        mFragments.add(new MyselfFragment());


        //初始化适配器
        mAdapter = new FragmentPagerAdapter(getSupportFragmentManager()) {

            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                // TODO Auto-generated method stub
                return mFragments.get(position);
            }
        };*/
/*
        //不要忘记设置ViewPager的适配器
        mViewPager.setAdapter(mAdapter);
        //设置ViewPager的切换监听
        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                // TODO Auto-generated method stub
                mViewPager.setCurrentItem(position);
                resetImgs();
                setSelect(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
                // TODO Auto-generated method stub

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {
                // TODO Auto-generated method stub

            }
        });*/
    }
    public void onResume() {
        getuserInfo();
        super.onResume();

    }
}
